package com.hmall.order.web;

import com.hmall.feign.client.ItemClient;
import com.hmall.feign.client.UserClient;
import com.hmall.item.pojo.Item;
import com.hmall.order.dto.OrderDto;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.service.OrderDetailService;
import com.hmall.order.service.OrderLogisticsService;
import com.hmall.order.service.OrderService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    OrderDetailService orderDetailService;
    @Autowired
    OrderLogisticsService orderLogisticsService;
    @PostMapping
    public Long order(@RequestBody OrderDto orderDto ) {
        Order order=orderService.inserOrder(orderDto);
       Long id= order.getId();
       orderDetailService.insertOrderDetail(orderDto,id);
        orderLogisticsService.insertOrderLogistics(orderDto.getAddressId(),id);
        orderService.updateStock(orderDto);
        return id;
    }
    @GetMapping("{id}")
    public Order queryById(@PathVariable Long id){
        return orderService.queryById(id);
    }
    @GetMapping("/pay/{type}/{orderId}")
    public String orderPay(@PathVariable Integer type,@PathVariable Long orderId) throws Exception {
        String codeUrl = orderService.orderPay(type, orderId);
        return codeUrl;
    }
    @PostMapping("/pay/notify")
    public Map<String, String> payNotify(HttpServletRequest request) throws IOException {
        System.out.println("=================== 微信之后成功回调方法 ===================");
        Map<String, String> map = orderService.payNotify(request.getInputStream());
        return map;
    }

}
